﻿using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using com.fukefu.sql;

namespace com.fukefu.tools
{
    public class TitleCollection
    {
        SqlServerOperate sql = new SqlServerOperate();

        Queue<Title> queue_title = new Queue<Title>();
        Hashtable ht_title = new Hashtable();

        public TitleCollection()
        {
            LoadAllTitle();
        }
        //加载所有关键字
        private void LoadAllTitle()
        {
            string cmd = "select a.kid,titleid,ktid,a.word from _bt_关键字 as a,_bt_标题 as b where a.kid=b.kid";
            SqlDataReader re = sql.Query(cmd);
            while (re.Read())
            {
                ht_title.Add(re["kid"].ToString(), new Title(re["ktid"].ToString(), re["titleid"].ToString(), re["word"].ToString()));
            }
            re.Close();
        }
        //加载指定站点没有排名的网址
        public void InitSite(Site site)
        {
            queue_title = new Queue<Title>();
            /**
             * 增加没有排名的关键字
             * 1、模板中要存在，即是kid
             * 2、最近没有发布过，发布日期限制是Config.PublicDays
             */
            string cmd = "select a.kid from _bt_关键字 as a left join (select distinct ktid from [_mb_模板]) as b on a.ktid=b.ktid left join (select kid from [已发布],[_site_账户] where [_site_账户].wid='" + site.Wid + "' and datediff(D,[已发布].dt,GETDATE())<" + site.TodayPublicNum + " and [已发布].kid is not null and [已发布].uid=[_site_账户].uid) as c on c.kid=a.kid left join (select distinct kid from _pm_排名 as a,_pm_网址 as b,_pm_特征查询结果 as c,_pm_特征 as d where a.dt=convert(varchar,getdate(),23) and a.pm<11 and b.domain='" + site.Domain + "' and a.uid=b.uid  and b.uid=c.uid and c.tid=d.tid and d.friends=1) as d on d.kid=a.kid where b.ktid is not null and c.kid is null and d.kid is null order by NEWID()";
            SqlDataReader re = sql.Query(cmd);
            while (re.Read())
            {
                queue_title.Enqueue(ht_title[re["kid"].ToString()] as Title);
            }
            re.Close();
            //重复发布最近一周没有发布的文章
            re = sql.Query("select a.kid from _bt_关键字 as a left join (select distinct ktid from [_mb_模板]) as b on a.ktid=b.ktid left join (select distinct kid from 已发布,_site_账户 where DATEDIFF(D,已发布.dt,GETDATE())<7 and _site_账户.wid='" + site.Wid + "' and 已发布.uid=_site_账户.uid) as c on a.kid=c.kid where b.ktid is not null and c.kid is null order by a.weights asc");
            while (re.Read())
            {
                if (!queue_title.Contains(ht_title[re["kid"].ToString()] as Title))
                {
                    queue_title.Enqueue(ht_title[re["kid"].ToString()] as Title);
                }
            }
            re.Close();
        }
        //获取标题
        public Title Get()
        {
            if (queue_title.Count > 0)
            {
                return queue_title.Dequeue();
            }
            return null;
        }
        public Title Get(string kid)
        {
            if (ht_title.Contains(kid))
            {
                return ht_title[kid] as Title;
            }
            return null;
        }
        //获取队列长度
        public int Count
        {
            get
            {
                return queue_title.Count;
            }
        }
    }
}
